package com.example.dragon.xmf;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.dragon.adpter.CompletedAdpter;
import com.example.dragon.adpter.ProblemAdpter;
import com.example.dragon.bean.OrderBean;
import com.example.dragon.bean.ProblemBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.untis.DateUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class ProblemActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ProblemAdpter problemAdpter;
    List<ProblemBean> problemBeanList = new ArrayList<>();
    private LinearLayout pback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem);
        initControls();
    }
    private void initControls(){
        recyclerView = (RecyclerView)findViewById(R.id.problem_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        pback = (LinearLayout)findViewById(R.id.pback) ;
        pback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProblemActivity.this.finish();
            }
        });
        getOrder();
    }

    private void getOrder(){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Help&a=getList")
                .addParams("nowpage","1")
                .addParams("perpage","12")
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                JSONObject datas = jsonObject.getJSONObject("datas");
                                JSONArray list = datas.getJSONArray("list");
                                for (int i=0;i<list.length();i++){
                                    JSONObject json = list.getJSONObject(i);
                                    problemBeanList.add(new ProblemBean(
                                            json.getString("title"),
                                            json.getString("content"),
                                            json.getString("addtime"),
                                            json.getString("help_id")
                                    ));
                                }
                                problemAdpter = new ProblemAdpter(getApplicationContext(),problemBeanList);
                                recyclerView.setAdapter(problemAdpter);
                            }else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
